
package com.crv.ci.action;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crv.common.domain.view.BizData4Page;
import com.crv.ci.model.CommandParams;
import com.crv.ci.service.ICommandParamsService;
import com.crv.common.utils.IdGenerator;
import com.crv.common.utils.JsonResult;
import com.crv.common.utils.PageParam;
import com.crv.common.utils.Constants;

@Controller
public class CommandParamsAction extends AbstractAdminController<ICommandParamsService>{

    @Autowired
    private ICommandParamsService commandParamsService;
    
    @RequestMapping(value="/commandparams/list")
    public String list(CommandParams commandParams,ModelMap model,PageParam pageParam,HttpServletRequest request,HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(commandParams); 	
    	BizData4Page<CommandParams> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("commandParams", commandParams);
    	return "/module/commandparams/list";
    }    
    
    @RequestMapping(value="/commandparams/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

    	model.addAttribute("commandParams", new CommandParams());
        return "module/commandparams/edit";
    }    
    
    @RequestMapping(value="/commandparams/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	CommandParams commandParams = commandParamsService.fetch(id);
    	model.addAttribute("commandParams", commandParams);
        return "module/commandparams/edit";
    }    
    
    @RequestMapping(value="/commandparams/save")
    @ResponseBody
    public String save(HttpServletRequest request,CommandParams commandParams){
		boolean isAddNew = StringUtils.isBlank(commandParams.getId())?true:false;
		if(isAddNew)
		{
			commandParams.setId(IdGenerator.createNewId());
			commandParamsService.add(commandParams);
		}
		else
		{
			commandParamsService.update(commandParams);
		}
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }    
    
    @RequestMapping(value="/commandparams/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	commandParamsService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }       
	
    protected Map getQueryMap(CommandParams commandParams)
    {
    	Map<String, Object> conditions = new HashMap();
    	
		conditions.put("paramName", commandParams.getParamName());		
		conditions.put("paramNameCn", commandParams.getParamNameCn());		
		conditions.put("cmdId", commandParams.getCmdId());		
		conditions.put("cmdName", commandParams.getCmdName());		
		conditions.put("cmdTitle", commandParams.getCmdTitle());		
		conditions.put("seq", commandParams.getSeq());		
		conditions.put("defaultValue", commandParams.getDefaultValue());		
		conditions.put("paramType", commandParams.getParamType());		
		conditions.put("note", commandParams.getNote());		
		conditions.put("createDate", commandParams.getCreateDate());		
    	return conditions;
    }

    @Override
    protected ICommandParamsService getMainService() {
        return commandParamsService;
    }

    @Override
    protected String getMainObjName() {
        return "commandparams";
    }

    @Override
    protected String getViewTitle() {
        return "commandparams";
    }
}
